<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

class frontenduseraccessViewBatchassign extends JView
{
	function display($tpl = null)
	{				
		$controller = new frontenduseraccessController();	
		$this->assignRef('controller', $controller);
		
		//only super admins should do this
		if($controller->user_type!='Super Administrator'){
			die('only super admins can do this');
		}
		
		$batchtype = $controller->get_var('batchtype', '', 'get');
		if($batchtype=='jf'){
			//joomlagroup to fua group
			
			$j = intval($controller->get_var('j', '', 'get', 'int'));
			$this->assignRef('j', $j);
			$f = intval($controller->get_var('f', '', 'get', 'int'));
			$this->assignRef('f', $f);
			
			//get users which need updating			
			$controller->db->setQuery( "SELECT id, username "
			. "FROM #__users "		
			. "WHERE gid='$j' "
			. "ORDER BY username ASC"
			);		
			
		}elseif($batchtype=='ff'){
			//fua group to fua group
						
			$f1 = intval($controller->get_var('f1', '', 'get', 'int'));
			$this->assignRef('f1', $f1);
			$f2 = intval($controller->get_var('f2', '', 'get', 'int'));
			$this->assignRef('f2', $f2);
			
			if($f1==$f2){
				global $mainframe;				
				$mainframe->redirect('index2.php?option=com_frontenduseraccess&view=config&tab=users', JText::_('NO_USERS_TO_RENDER'));	
			}			
			
			if($f1=='0'){
				
				//get userid's from index
				$controller->db->setQuery( "SELECT user_id "
				. "FROM #__fua_userindex "		
				);
				$fua_user_ids = $controller->db->loadResultArray();
				$fua_ids_string = '';
				$first = 1;
				foreach($fua_user_ids as $fua_user_id){
					if(!$first){
						$fua_ids_string .= ',';
					}
					$fua_ids_string .= $fua_user_id;
					$first = 0;
				}
				
				$controller->db->setQuery( "SELECT id, username "
				. "FROM #__users "				
				. "WHERE (id NOT IN ($fua_ids_string)) AND (usertype<>'Super Administrator') "
				. "ORDER BY username ASC "
				);	
				
			}else{			
				//get users which need updating	
				$controller->db->setQuery( "SELECT u.id as id, u.username "
				. "FROM #__users AS u "		
				. "LEFT JOIN #__fua_userindex AS i "
				. "ON i.user_id=u.id "
				. "WHERE i.group_id='$f1' "
				. "ORDER BY username ASC "
				);	
			}
					
			
		}else{
			die('no batch type');
		}
		$this->assignRef('batchtype', $batchtype);	
		$users = $controller->db->loadObjectList();
		$this->assignRef('users', $users);
		
		/*
		echo 'f1='.$f1;
		echo '<br />';
		echo 'f2='.$f2;
		echo '<br />';
		print_r($users);
		
		exit;
		*/
		
		
		$total_to_render = count($users);		
		$this->assignRef('total_to_render', $total_to_render);		
		if($total_to_render==0){	
			global $mainframe;				
			$mainframe->redirect('index2.php?option=com_frontenduseraccess&view=config&tab=users', JText::_('NO_USERS_TO_RENDER'));		
		}
	
		parent::display($tpl);
	}

}
?>